package mypazar.customer;

import jason.environment.grid.Location;
import mypazar.agent.PersonAgent;
import mypazar.jenamodel.PazarVocabulary;
import tr.edu.ege.seagent.planner.htn.Action;
import tr.edu.ege.seagent.planner.htn.ExecutionMethod;
import tr.edu.ege.seagent.planner.htn.Outcome;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * Action class of ACOnStall.
 */
public class ACOnStall extends Action {

	/**
	 * SalesmanName outcome.
	 */
	@Outcome
	private String StallArtifactName;

	/**
	 * Returns the value of the SalesmanName outcome.
	 */
	public String getStallArtifactName() {
		return this.StallArtifactName;
	}

	/**
	 * Sets the value of the SalesmanName outcome.
	 */
	public void setStallArtifactName(String StallArtifactName) {
		this.StallArtifactName = StallArtifactName;
	}

	/**
	 * Execution method.
	 */
	@ExecutionMethod
	public void execute() {
		Model beliefBase = (Model) getHeapValue(PersonAgent.BELIEF_BASE_ITEM_NAME);

		String PERSON_URI = PazarVocabulary.NS_URI
				+ getAgentIdentifier().toString().substring(0, 7);

		Resource resPerson = beliefBase.getResource(PERSON_URI);

		Location currentLoc = getCurrentLoc(resPerson);

		String smanName = setAsSeenAndGetSmanName(beliefBase, PERSON_URI,
				currentLoc);

		String stallArtifactName = "Stall" + smanName.substring(8, 9);

		this.setStallArtifactName(stallArtifactName);
		// JOptionPane.showMessageDialog(null, "ACOnStall");
		this.setOutcomeState("Query");
	}

	/**
	 * Gets agent's current location from belief base
	 * 
	 * @param beliefBase
	 * @param personURI
	 * @return
	 */
	private Location getCurrentLoc(Resource resPerson) {
		StmtIterator iterPos = resPerson.listProperties(ResourceFactory
				.createProperty(PazarVocabulary.POSITION_URI));
		if (iterPos.hasNext()) {
			Statement stmtPersonPos = iterPos.next();
			Resource resPersonStall = stmtPersonPos.getObject().asResource();
			// gets and returns location from given resource
			return getResLocation(resPersonStall);

		} else
			return null;
	}

	/**
	 * Gets given resource's location
	 * 
	 * @param resPersonStall
	 * @return
	 */
	private Location getResLocation(Resource resPersonStall) {
		// gets and returns location from given iteration
		return new Location(resPersonStall
				.getProperty(
						ResourceFactory
								.createProperty(PazarVocabulary.POS_X_URI))
				.getLiteral().getInt(), resPersonStall
				.getProperty(
						ResourceFactory
								.createProperty(PazarVocabulary.POS_Y_URI))
				.getLiteral().getInt());
	}

	/**
	 * Sets stall that located agent's current location as seen stall
	 * 
	 * @param beliefBase
	 * @param personURI
	 * @param currentLoc
	 * @return
	 */
	public String setAsSeenAndGetSmanName(Model beliefBase, String personURI,
			Location currentLoc) {
		// JOptionPane.showMessageDialog(null, "PERSONURI: " + personURI);
		Resource resPerson = beliefBase.getResource(personURI);

		StmtIterator iterStall = resPerson.listProperties(ResourceFactory
				.createProperty(PazarVocabulary.STALL_URI));
		// JOptionPane
		// .showMessageDialog(null, "iSHasNext: " + iterStall.hasNext());
		while (iterStall.hasNext()) {
			Statement stmtPersonStall = iterStall.next();
			Resource resPersonStall = stmtPersonStall.getObject().asResource();

			if (currentLoc.equals(getResLocation(resPersonStall))) {
				resPersonStall.addProperty(beliefBase
						.createProperty(PazarVocabulary.STALL_SEEN_URI),
						PersonAgent.YES_STR, XSDDatatype.XSDstring);

				return resPersonStall
						.getProperty(
								ResourceFactory
										.createProperty(PazarVocabulary.HAS_SALESMAN_URI))
						.getLiteral().getString();
			}
		}
		return null;
	}
}
